package com.aiyige.page.publish.traingcard.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.aiyige.model.moment.backup.CourseBackup;
import com.aiyige.model.moment.backup.TagBackup;
import com.aiyige.model.moment.entity.Guarantee;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.model.moment.entity.TrainingCard;
import com.aiyige.page.publish.coursedescription.model.CourseCover;
import com.aiyige.page.publish.coursedescription.model.CourseDescriptionModel;
import com.aiyige.page.publish.guarantee.model.GuaranteeItem;
import com.aiyige.page.publish.selectcourse.model.Course;
import com.aiyige.page.publish.selecttag.model.SelectTagItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.setup.util.SetupUtil;
import com.aiyige.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2018/3/23.
 */

public class
TraingCardFormModel implements Parcelable {
    public static final int CARD_TYPE_UNKNOWN = -1;
    public static final int CARD_TYPE_TIME_CARD = 1;
    public static final int CARD_TYPE_COUNT_CARD = 2;
    public static final int CARD_TYPE_PREPAID_CARD = 3;
    public static final int EFFECTIVE_TYPE_IMMEDIATE = 1;
    public static final int EFFECTIVE_TYPE_ATTENDANCE = 2;
    String goodsId;
    String title;
    Integer cardType;
    String usageCount;
    Integer effectiveType;
    String validity;
    String price;
    String platformDividend;
    List<SelectTagItem> selectTagItemList;
    List<Course> courseList;
    CourseDescriptionModel courseDescriptionModel;
    CourseCover cover;
    int operationType;
    List<GuaranteeItem> guaranteeItemList;

    private TraingCardFormModel(Builder builder) {
        setGoodsId(builder.goodsId);
        setTitle(builder.title);
        setCardType(builder.cardType);
        setUsageCount(builder.usageCount);
        setEffectiveType(builder.effectiveType);
        setValidity(builder.validity);
        setPrice(builder.price);
        setPlatformDividend(builder.platformDividend);
        setSelectTagItemList(builder.selectTagItemList);
        setCourseList(builder.courseList);
        setCourseDescriptionModel(builder.courseDescriptionModel);
        setCover(builder.cover);
        setOperationType(builder.operationType);
        setGuaranteeItemList(builder.guaranteeItemList);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getCardType() {
        return cardType;
    }

    public void setCardType(Integer cardType) {
        this.cardType = cardType;
    }

    public String getUsageCount() {
        return usageCount;
    }

    public void setUsageCount(String usageCount) {
        this.usageCount = usageCount;
    }

    public Integer getEffectiveType() {
        return effectiveType;
    }

    public void setEffectiveType(Integer effectiveType) {
        this.effectiveType = effectiveType;
    }

    public String getValidity() {
        return validity;
    }

    public void setValidity(String validity) {
        this.validity = validity;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getPlatformDividend() {
        return platformDividend;
    }

    public void setPlatformDividend(String platformDividend) {
        this.platformDividend = platformDividend;
    }

    public List<SelectTagItem> getSelectTagItemList() {
        return selectTagItemList;
    }

    public void setSelectTagItemList(List<SelectTagItem> selectTagItemList) {
        this.selectTagItemList = selectTagItemList;
    }

    public List<Course> getCourseList() {
        return courseList;
    }

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    public CourseDescriptionModel getCourseDescriptionModel() {
        return courseDescriptionModel;
    }

    public void setCourseDescriptionModel(CourseDescriptionModel courseDescriptionModel) {
        this.courseDescriptionModel = courseDescriptionModel;
    }

    public CourseCover getCover() {
        return cover;
    }

    public void setCover(CourseCover cover) {
        this.cover = cover;
    }

    public int getOperationType() {
        return operationType;
    }

    public void setOperationType(int operationType) {
        this.operationType = operationType;
    }

    public List<GuaranteeItem> getGuaranteeItemList() {
        return guaranteeItemList;
    }

    public void setGuaranteeItemList(List<GuaranteeItem> guaranteeItemList) {
        this.guaranteeItemList = guaranteeItemList;
    }

    public static final class Builder {
        private String goodsId;
        private String title;
        private Integer cardType;
        private String usageCount;
        private Integer effectiveType;
        private String validity;
        private String price;
        private String platformDividend;
        private List<SelectTagItem> selectTagItemList;
        private List<Course> courseList;
        private CourseDescriptionModel courseDescriptionModel;
        private CourseCover cover;
        private int operationType;
        private List<GuaranteeItem> guaranteeItemList;

        private Builder() {
            goodsId = null;
            title = null;
            cardType = TraingCardFormModel.CARD_TYPE_COUNT_CARD;
            usageCount = null;
            effectiveType = null;
            validity = null;
            price = null;
            platformDividend = "";
            selectTagItemList = new LinkedList<>();
            courseList = new LinkedList<>();
            courseDescriptionModel = CourseDescriptionModel.newBuilder().build();
            cover = null;
            operationType = PublishUtil.OPERATION_TYPE_CREATE;
            guaranteeItemList = new LinkedList<>();
        }

        public Builder goodsId(String goodsId) {
            this.goodsId = goodsId;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder cardType(Integer cardType) {
            this.cardType = cardType;
            return this;
        }

        public Builder usageCount(String usageCount) {
            this.usageCount = usageCount;
            return this;
        }

        public Builder effectiveType(Integer effectiveType) {
            this.effectiveType = effectiveType;
            return this;
        }

        public Builder validity(String validity) {
            this.validity = validity;
            return this;
        }

        public Builder price(String price) {
            this.price = price;
            return this;
        }

        public Builder platformDividend(String platformDividend) {
            this.platformDividend = platformDividend;
            return this;
        }

        public Builder selectTagItemList(List<SelectTagItem> selectTagItemList) {
            this.selectTagItemList = selectTagItemList;
            return this;
        }

        public Builder courseList(List<Course> courseList) {
            this.courseList = courseList;
            return this;
        }

        public Builder courseDescriptionModel(CourseDescriptionModel courseDescriptionModel) {
            this.courseDescriptionModel = courseDescriptionModel;
            return this;
        }

        public Builder cover(CourseCover cover) {
            this.cover = cover;
            return this;
        }

        public Builder operationType(int operationType) {
            this.operationType = operationType;
            return this;
        }

        public Builder guaranteeItemList(List<GuaranteeItem> guaranteeItemList) {
            this.guaranteeItemList = guaranteeItemList;
            return this;
        }

        public TraingCardFormModel build() {
            return new TraingCardFormModel(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.goodsId);
        dest.writeString(this.title);
        dest.writeValue(this.cardType);
        dest.writeString(this.usageCount);
        dest.writeValue(this.effectiveType);
        dest.writeString(this.validity);
        dest.writeString(this.price);
        dest.writeString(this.platformDividend);
        dest.writeTypedList(this.selectTagItemList);
        dest.writeTypedList(this.courseList);
        dest.writeParcelable(this.courseDescriptionModel, flags);
        dest.writeParcelable(this.cover, flags);
        dest.writeInt(this.operationType);
        dest.writeTypedList(this.guaranteeItemList);
    }

    protected TraingCardFormModel(Parcel in) {
        this.goodsId = in.readString();
        this.title = in.readString();
        this.cardType = (Integer) in.readValue(Integer.class.getClassLoader());
        this.usageCount = in.readString();
        this.effectiveType = (Integer) in.readValue(Integer.class.getClassLoader());
        this.validity = in.readString();
        this.price = in.readString();
        this.platformDividend = in.readString();
        this.selectTagItemList = in.createTypedArrayList(SelectTagItem.CREATOR);
        this.courseList = in.createTypedArrayList(Course.CREATOR);
        this.courseDescriptionModel = in.readParcelable(CourseDescriptionModel.class.getClassLoader());
        this.cover = in.readParcelable(CourseCover.class.getClassLoader());
        this.operationType = in.readInt();
        this.guaranteeItemList = in.createTypedArrayList(GuaranteeItem.CREATOR);
    }

    public static final Parcelable.Creator<TraingCardFormModel> CREATOR = new Parcelable.Creator<TraingCardFormModel>() {
        @Override
        public TraingCardFormModel createFromParcel(Parcel source) {
            return new TraingCardFormModel(source);
        }

        @Override
        public TraingCardFormModel[] newArray(int size) {
            return new TraingCardFormModel[size];
        }
    };

    public static TraingCardFormModel parse(Moment moment, int operationType) {
        List<TagBackup> tagBackupList = moment.getTags();
        List<SelectTagItem> selectTagItemList = new LinkedList<>();
        for (TagBackup tagBackup : tagBackupList) {
            if (tagBackup == null) continue;
            switch (tagBackup.getGroupType()) {
                case SetupUtil.GROUP_TYPE_OTHER:
                    selectTagItemList.add(SelectTagItem.newBuilder()
                            .id(tagBackup.getId())
                            .name(tagBackup.getName())
                            .build());
                    break;
            }
        }
        List<GuaranteeItem> guaranteeItemList = new LinkedList<>();
        List<Guarantee> guaranteeList = moment.getDetailBackup().getGuarantee();
        for (Guarantee guarantee : guaranteeList) {
            guaranteeItemList.add(GuaranteeItem.newBuilder()
                    .id(guarantee.getId())
                    .title(guarantee.getName())
                    .content(guarantee.getDetail())
                    .build());
        }
        TrainingCard trainingCard = JSON.parseObject(moment.getMoreBackup(), TrainingCard.class);
        List<Course> courseList = new LinkedList<>();
        try {
            List<CourseBackup> courseBackupList = trainingCard.getBindCourseBackup();
            for (CourseBackup courseBackup : courseBackupList) {
                int courseType;
                switch (courseBackup.getSubject()) {
                    case Moment.SUBJECT_MAJOR_COURSE:
                        courseType = Course.TYPE_MAJOR_COURSE;
                        break;
                    case Moment.SUBJECT_TRAINING_COURSE:
                        courseType = Course.TYPE_TRAING_CLASS;
                        break;
                    default:
                        courseType = Course.TYPE_MAJOR_COURSE;
                        break;
                }
                courseList.add(Course.newBuilder()
                        .id(courseBackup.getId())
                        .type(courseType)
                        .title(courseBackup.getTitle())
                        .singlePrice(courseBackup.getBindUnitPrice() == null ? null : StringUtils.priceFormatEscapeRedundantTailZero(courseBackup.getBindUnitPrice()))
                        .courseNum(courseBackup.getClassCount() == null ? null : String.valueOf(courseBackup.getClassCount()))
                        .totalPrice(courseBackup.getPrice() == null ? null : StringUtils.priceFormatEscapeRedundantTailZero(courseBackup.getPrice()))
                        .build());
            }
        } catch (Exception e) {

        }

        CourseDescriptionModel courseDescriptionModel = CourseDescriptionModel.newBuilder().build();
        try {
            courseDescriptionModel.setCourseCoverList(PublishUtil.convertContentToCourseCover(trainingCard.getCoverSet()));
        } catch (Exception e) {

        }
        CourseCover defaultCover = null;
        for (CourseCover courseCover : courseDescriptionModel.getCourseCoverList()) {
            if (courseCover.isDefaultCover()) {
                defaultCover = courseCover;
                break;
            }
        }
        if (defaultCover == null) {
            defaultCover = CourseCover.newBuilder()
                    .imageUrl("")
                    .videoUrl("")
                    .needUpload(false)
                    .build();
        }

        try {
            courseDescriptionModel.setArticleNodeList(PublishUtil.convertContentToArticle(moment.getDetailBackup().getContent()));
        } catch (Exception e) {

        }

        String goodsId;
        switch (operationType) {
            case PublishUtil.OPERATION_TYPE_CREATE:
            case PublishUtil.OPERATION_TYPE_COPY:
                goodsId = null;
                break;
            case PublishUtil.OPERATION_TYPE_EDIT:
                goodsId = moment.getId();
                break;
            default:
                goodsId = null;
                break;
        }
        Integer cardType = null;
        try {
            switch (trainingCard.getCardType()) {
                case Moment.TRAINING_CARD_TYPE_TIME_CARD:
                    cardType = TraingCardFormModel.CARD_TYPE_TIME_CARD;
                    break;
                case Moment.TRAINING_CARD_TYPE_RECHARGEABLE_CARD:
                    cardType = TraingCardFormModel.CARD_TYPE_PREPAID_CARD;
                    break;
                case Moment.TRAINING_CARD_TYPE_MEASURED_CARD:
                    cardType = TraingCardFormModel.CARD_TYPE_COUNT_CARD;
                    break;
            }
        } catch (Exception e) {

        }
        Integer effectiveType = null;
        try {
            switch (trainingCard.getTakeEffectType()) {
                case Moment.TRAINING_CARD_TAKE_EFFECT_TYPE_ATTENDANCE:
                    effectiveType = TraingCardFormModel.EFFECTIVE_TYPE_ATTENDANCE;
                    break;
                case Moment.TRAINING_CARD_TAKE_EFFECT_TYPE_IMMEDIATE:
                    effectiveType = TraingCardFormModel.EFFECTIVE_TYPE_IMMEDIATE;
                    break;
            }
        } catch (Exception e) {

        }

        return TraingCardFormModel.newBuilder()
                .goodsId(goodsId)
                .title(moment.getTitle())
                .cardType(cardType)
                .usageCount(trainingCard.getUsageCount() == null ? null : String.valueOf(trainingCard.getUsageCount()))
                .effectiveType(effectiveType)
                .validity(trainingCard.getTakeEffectPeriod() == null ? null : String.valueOf(trainingCard.getTakeEffectPeriod()))
                .price(trainingCard.getPrice() == null ? null : StringUtils.priceFormatEscapeRedundantTailZero(trainingCard.getPrice()))
                .platformDividend(trainingCard.getDivisionProportion() == null ? StringUtils.priceFormatEscapeRedundantTailZero(PublishUtil.MIN_PLATFORM_DIVIDEND * 100) : StringUtils.platformDividendFormat(trainingCard.getDivisionProportion() * 100))
                .selectTagItemList(selectTagItemList)
                .courseList(courseList)
                .courseDescriptionModel(courseDescriptionModel)
                .cover(defaultCover)
                .operationType(operationType)
                .guaranteeItemList(guaranteeItemList)
                .build();
    }
}
